<?php





namespace app\admin\model;





use app\admin\validate\Store;

use think\model\concern\SoftDelete;



class StoreModel extends Common

{



    use SoftDelete;



    const FIELD = [

        'id', 'name', 'kingdee_code','market_name','market_id'

    ];

    // 设置字段信息

    protected $schema = [



    ];

    /**

     * 类型转换

     * @var string[]

     */

    protected $type = [



    ];

    /**

     * @var string

     */

    protected $name = 'shop_store';



    protected $defaultSoftDelete = 0;



    protected $autoWriteTimestamp = true;



    /**

     * 驼峰格式转换

     * @var bool

     */

    protected $convertNameToCamel = true;



    protected $fail;



    /**

     * 创建店铺市场资源

     * @author ran

     * @date 2021-09-22 16:37

     * mailbox 466180170@qq.com

     */

    public function createShopAdminStoreResource(array $request)

    {

        $validate = new Store();

        if (!$validate->scene('create')->check($this->humpToUnderscore($request))) {

            $this->error = $validate->getError();

            return false;

        }

        try {

            $res = $this->create($request);

            if ($res->id) {

                return true;

            }

            $this->error = '店铺仓库资源入库失败';

            return false;

        } catch (\Exception $e) {

            $this->error = $e->getMessage();

            return false;

        }

    }

    /**

     * 更新店铺市场资源

     * @author ran

     * @date 2021-09-22 16:37

     * mailbox 466180170@qq.com

     */

    public function updateShopAdminStoreResource(array $request)

    {

        $validate = new Store();

        if (!$validate->scene('update')->check($request)) {

            $this->error = $validate->getError();

            return false;

        }

        try {

            $this->update($request, ['id' => $request['id']]);

            return true;

        } catch (\Exception $e) {

            $this->error = $e->getMessage();

            return false;

        }

    }

    /**

     * 获取店铺仓库

     * @author ran

     * @date 2021-09-22 16:37

     * mailbox 466180170@qq.com

     */

    public function getShopAdminStoreListResource($request,int $page, int $limit = 10)

    {

        $where = [];

        $marketId = isset($request['marketId']) && intval($request['marketId']) ? intval($request['marketId']) : 0;

        $marketId && array_push($where, ['market_id', '=', $marketId]);

        $connect = $this->where($where)->field(self::FIELD);

        return [

            'list' => $connect->page($page, $limit)->select(),

            'dataCount' => $this->count()

        ];

    }

    /**

     * 根据店铺ID获取市场关联仓库集合

     * @author ran

     * @date 2021-11-29 12:58

     * mailbox 466180170@qq.com

     */

    public function getAdminStoreWithMarketListByShopId(int $shopId)

    {

        $shopModel = new ShopModel();

        $shop =$shopModel->field('market_id,relation_market_id,is_sign,store_id')->where('shop_id',$shopId)->find();

        switch ($shop['is_sign']) {

            case 2:

               /* $marketIds =[$shop['marketId']];

                if(!empty($shop['relationMarketId'])){

                    array_push($marketIds,$shop['relationMarketId']);

                }

                $connect =$this->field(self::FIELD)

                    ->when($marketIds ?? [], function ($query) use ($marketIds) {

                        return $query->whereIn('market_id', $marketIds);

                    });

                $count = $connect->count();*/

                $connect =$this->field(self::FIELD)->where('market_id',$shop['market_id']);

                $count = $connect->count();

                break;

            default:

                $connect =$this->field(self::FIELD)->where('id',$shop['store_id']);

                $count = $connect->count();

                break;

        }

        return [

            'list' => $connect->select(),

            'dataCount' =>$count

        ];

    }

    /**

     * 获取 员工信息

     * @param int $id

     * @author ran

     * @date 2021-10-18 9:02

     * mailbox 466180170@qq.com

     */

    public function getShopAdminStoreInfoResource(int $id)

    {

        $field = ['id', 'name','market_id','kingdee_code'];

        return $this->field($field)->find($id);

    }



}